package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.impl.ClazzServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RequestMapping("/clazzs")
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    //3.1班级列表查询
    @GetMapping
    public Result getPage(ClazzQueryParam queryParam) {
        //1.接收kv格式的参数
        log.info("分页查询，参数：{}",queryParam);
        //2.调用业务层方法，获取分页数据
        //3.返回指定的数据
        return Result.success(clazzService.pageClazz(queryParam));
    }

    //3.2 删除班级
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除班级，id：{}",id);
        clazzService.deleteClazz(id);
        return Result.success();
    }
    //3.3 添加班级
    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        //填补基本信息
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("添加班级，参数：{}",clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    //3.4根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级，id：{}",id);
        return Result.success(clazzService.getById(id));
    }

    //3.5修改班级
    @PutMapping
    public Result updateById(@RequestBody Clazz clazz) {
        //填补基本信息
        clazz.setUpdateTime(LocalDateTime.now());
        log.info("修改班级，参数：{}",clazz);
        LambdaQueryWrapper<Clazz> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Clazz::getId,clazz.getId());
        clazzService.update(clazz,queryWrapper);
        return Result.success();
    }

    //3.6查询所有班级
    @GetMapping("/list")
    public Result selectAllClazz() {
        log.info("查询所有班级");
        return Result.success(clazzService.list());
    }




    /*//3.1班级列表查询
    @GetMapping
    public Result getPage(ClazzQueryParam queryParam) {
        //1.接收kv格式的参数
        log.info("分页查询，参数：{}",queryParam);
        //2.调用业务层方法，获取分页数据
        ClazzResult clazzResult = clazzService.getPage(queryParam);
        //3.返回指定的数据
        return Result.success(clazzResult);
    }

    //3.2 删除班级
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除班级，id：{}",id);
        clazzService.deleteById(id);
        return Result.success();
    }
    //3.3 添加班级
    @PostMapping
    public Result insertClazz(@RequestBody Clazz clazz) {
        log.info("添加班级，参数：{}",clazz);
        clazzService.insertClazz(clazz);
        return Result.success();
    }

    //3.4根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("根据id查询班级，id：{}",id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    //3.5修改班级
    @PutMapping
    public Result updateById(@RequestBody Clazz clazz) {
        log.info("修改班级，参数：{}",clazz);
        clazzService.updateById(clazz);
        return Result.success();
    }

    //3.6查询所有班级
    @GetMapping("/list")
    public Result selectAllClazz() {
        log.info("查询所有班级");
        return Result.success(clazzService.selectAllClazz());
    }*/
}
